/*业务逻辑(左侧加号展开阅读):
 * 用户数据更新程序
 * 1. 获取前台jsp页面用户输入的信息
 * 2. 判断用户两次密码输入是否一致
 * 3. 将用户输入的数据封装至Customers对象中
 * 4. 将数据上传至数据库
 */

package OSMS.action.User;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


import com.sun.xml.internal.ws.resources.HttpserverMessages;

import OSMS.entity.Customers;
import OSMS.dao.*;
public class Cus_Update_Servlet extends HttpServlet {
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try{
//------------------1.获取信息页面的数据----------------------//
			request.setCharacterEncoding("UTF-8");
			response.setCharacterEncoding("utf-8");
			response.setContentType("text/html;charset=utf-8");
			
			String cid = request.getParameter("cid");
			String cname = new String(request.getParameter("cname").getBytes("utf-8"), "utf-8");
			String tel = new String(request.getParameter("tel").getBytes("utf-8"), "utf-8");
			String address = new String(request.getParameter("address").getBytes("utf-8"), "utf-8");
			String sex = new String(request.getParameter("sex").getBytes("utf-8"), "utf-8");
			String cpwd = request.getParameter("cpwd");
			String cpwd_assure = request.getParameter("cpwd_a");
			
//------------------2.验证两次密码是否正确--------------------//
			if(!cpwd.equals(cpwd_assure))
			{
				request.getRequestDispatcher("updatecus.jsp").forward(request, response);
			}
//------------------3.封装前台数据---------------------------//
			Customers cus = new Customers();
			HttpSession session = request.getSession();
			Object obj = session.getAttribute("cus");
			cus = (Customers)obj;
			cus.setCname(cname);
			cus.setTel(tel);
			cus.setAddress(address);
			cus.setSex(sex);
			cus.setCpwd(cpwd);
//------------------4.上传至数据库--------------------------//
			CustomerDao cdao = new CustomerDao();
			boolean flag = false;
			flag = cdao.updateCus(cus);
			request.getRequestDispatcher("cusindex.jsp").forward(request, response);
		}catch(Exception ex){
			ex.printStackTrace();
		}
		
		
	}
	
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}
	
	
	
}